package sjtu.ist.warehouse4ws.tool.parser;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import sjtu.ist.warehouse4ws.model.Department;
import sjtu.ist.warehouse4ws.model.Domainmodelstate;
import sjtu.ist.warehouse4ws.model.Fileinfo;
import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.model.Functions;
import sjtu.ist.warehouse4ws.model.Permission;
import sjtu.ist.warehouse4ws.model.Permissionresources;
import sjtu.ist.warehouse4ws.model.Resource;
import sjtu.ist.warehouse4ws.model.Role;
import sjtu.ist.warehouse4ws.model.Rolepermissions;
import sjtu.ist.warehouse4ws.tool.ParserOrg;
import sjtu.ist.warehouse4ws.tool.file.ModelPackage;

public class PackageParser {
	public static final String newline = "\n";// 显示文本换行符
	private ModelPackage mp;// 模型包
	private StringBuilder result;// 一致性检查结果
	private String groupName="";

	//private List<Fileinfo> checkConsistence = new ArrayList<Fileinfo>();
	private List<Domainmodelstate> domainStateEpc;
	private List<Functionpoint> functionEpc;
	private HashMap<String, Integer> functionPoint;

	public PackageParser(File pkg, String dest, String pkgName) {
		mp = new ModelPackage(pkg, dest);		
		
		int dot=pkgName.indexOf(".");
		groupName=pkgName.substring(0,dot);
	}

	/**
	 * 检测模型一致性
	 * 
	 * @param mp
	 * @return
	 */
	public void checkConsistency() {
		result = new StringBuilder();

		if (mp.processFiles.size() == 0)
			result.append("缺少功能结构模型" + newline);
		if (mp.functionFiles.size() == 0)
			result.append("缺少功能模型" + newline);
		if (mp.organizationFiles.size() == 0)
			result.append("缺少组织模型" + newline);
		if (mp.domainFiles.size() == 0)
			result.append("业务对象模型" + newline);
		if (mp.epcFiles.size() == 0)
			result.append("缺少epc模型" + newline);
		
		result.append(checkDomainModels());
		result.append(checkFunctionModels());
		result.append(checkProcessModels());
		result.append(checkDomainModels());
		result.append(checkEpcModels());
	}

	/**
	 * 检测领域模型
	 * 
	 * @return
	 */
	public String checkDomainModels() {
		result = new StringBuilder();
		List<Fileinfo> domain = new ArrayList<Fileinfo>();
		domainStateEpc = new ArrayList<Domainmodelstate>();

		for (File f : mp.domainFiles) {
			Fileinfo fileinfo = new Fileinfo();
			fileinfo.setXmlpath(f.getAbsolutePath());
			fileinfo.setName(f.getName());
			fileinfo.setId(java.util.UUID.randomUUID().toString());
			fileinfo.setType(4);
			fileinfo.setProject(groupName);
			domain.add(fileinfo);			

			// 解析模型文件
			DomainModelParser dmp = new DomainModelParser(f);
			dmp.parse();

			// 解析state
			ArrayList<Domainmodelstate> dmodelstate = dmp.getStateList();
			for (Domainmodelstate ds : dmodelstate) {
				domainStateEpc.add(ds);
			}

			String checkDMResult = dmp.checkDM(mp);
			if (!checkDMResult.equals("")) {
				result.append(checkDMResult + newline);
			}
		}

		return result.toString();
	}

	/**
	 * 检测功能模型
	 * 
	 * @return
	 */
	public String checkFunctionModels() {
		StringBuilder result = new StringBuilder();
		functionPoint = new HashMap<String, Integer>();
		functionEpc = new ArrayList<Functionpoint>();
		for (File f : mp.functionFiles) {
			Fileinfo fileinfo = new Fileinfo();
			fileinfo.setXmlpath(f.getAbsolutePath());
			fileinfo.setName(f.getName());
			fileinfo.setId(java.util.UUID.randomUUID().toString());
			fileinfo.setType(2);
			fileinfo.setProject(groupName);

			FunctionPointParser fpp = new FunctionPointParser(f);
			fpp.parse();
			Functionpoint fp = fpp.getFunctionPoint(); // 抽取所有功能点
			functionPoint.put(fp.getId(), 0);
			functionEpc.add(fp);			
		}

		return result.toString();
	}

	/**
	 * 检测流程模型
	 * 
	 * @return
	 */
	public String checkProcessModels() {
		StringBuilder result = new StringBuilder();

		for (File f : mp.processFiles) {
			Fileinfo fileinfo = new Fileinfo();
			fileinfo.setXmlpath(f.getAbsolutePath());
			fileinfo.setName(f.getName());
			fileinfo.setId(java.util.UUID.randomUUID().toString());
			fileinfo.setType(1);
			fileinfo.setProject(groupName);

			ProcessParser pp = new ProcessParser(f);
			pp.parse();

			ArrayList<Functions> fs = pp.getFunList();

			for (Functions functions : fs) { // 检测流程文件的功能点是否都已上传
				String fId = functions.getFunctionid();
				if (functionPoint.containsKey(fId))
					functionPoint.put(fId, 1);
				else
					result.append(fId + "功能文件没有上传" + newline);
			}
		}

		if (functionPoint.containsValue(0)) {
			// 采用Iterator遍历HashMap
			Iterator it = functionPoint.keySet().iterator();
			while (it.hasNext()) {

				String key = (String) it.next();

				if (functionPoint.get(key) == 0) {
					result.append(key);
					result.append("缺少对应的流程文件。" + newline);
				}
			}
		}

		return result.toString();
	}

	/**
	 * 检测EPC模型
	 * 
	 * @return
	 */
	public String checkEpcModels() {
		StringBuilder result = new StringBuilder();

		for (File f : mp.epcFiles) {
			Fileinfo fileinfo = new Fileinfo();
			fileinfo.setXmlpath(f.getAbsolutePath());
			fileinfo.setName(f.getName());
			fileinfo.setId(java.util.UUID.randomUUID().toString());
			fileinfo.setType(5);
			fileinfo.setProject(groupName);
		
			EpcParser ep=new EpcParser(f);	
			ep.parse();
			result.append(ep.checkEpcConsistence(domainStateEpc, functionEpc));
		}

		return result.toString();
	}

	/**
	 * @return the mp
	 */
	public ModelPackage getMp() {
		return mp;
	}

	/**
	 * @return the result
	 */
	public String getResult() {
		return result.toString();
	}

//	public List<Fileinfo> getCheckConsistence() {
//		return checkConsistence;
//	}
}
